home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / jogos / spherical / Code / Game / file_io.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-07-30  |  10.9 KB  |  384 lines

  1.  
  2. // Copyright (C) 2002 by Luigi Pino.  All Rights Reserved.
  3.  
  4. /***************************************************************************/
  5.  
  6. #include "../library/neurosis.h"
  7. #include "draw.h"
  8. #include "file_io.h"
  9. #include "hiscore.h"
  10. #include "initialize.h"
  11. #include "main.h"
  12. #include "menu_misc.h"
  13. #include "paddle.h"
  14. #include "sphere.h"
  15. #include "update.h"
  16.  
  17. /***************************************************************************/
  18.  
  19. extern bool                                connected[];
  20.  
  21. extern High_Score_Struct    high_score_sphere;
  22. extern High_Score_Struct    high_score_time;
  23. extern Values_Struct            values;
  24. extern Sub_Menu_Class            menu_options;
  25. extern Sub_Menu_Class            menu_control;
  26. extern Window_Class                window;
  27. extern char                                high_score_name[];
  28.  
  29. /***************************************************************************/
  30.  
  31. void Load_Config_File()
  32.  
  33. {
  34.     FILE    *stream;
  35.     int        x1;
  36.     int        next_char;
  37.     int        controller_value;
  38.  
  39.     if ((stream = fopen("config.ini", "r")) == NULL)
  40.         {
  41.             // Options
  42.             menu_options.Set_Current_Choice(0, 0);                    // Sound = On
  43.             menu_options.Set_Current_Choice(1, 0);                    // Resolution = 640 x 480
  44.             window.screen_height = 480;
  45.             window.screen_width = 640;
  46.             menu_options.Set_Current_Choice(2, 0);                    // Timed amount = 5
  47.             menu_options.Set_Current_Choice(3, 0);                    // Countdown amount = 300
  48.             strncpy(high_score_name, "LP23", 15);
  49.  
  50.             // Controls
  51.             menu_control.Set_Current_Choice(Up, Computer);
  52.             menu_control.Set_Current_Choice(Down, Keyboard_1);
  53.             menu_control.Set_Current_Choice(Left, Computer);
  54.             menu_control.Set_Current_Choice(Right, Computer);
  55.             menu_control.Set_Current_Choice(Compete, Keyboard_1);
  56.  
  57.             // High scores
  58.             strncpy(high_score_sphere.name[0], "---------", 15);
  59.             strncpy(high_score_sphere.name[1], "---------", 15);
  60.             strncpy(high_score_sphere.name[2], "---------", 15);
  61.             strncpy(high_score_sphere.name[3], "---------", 15);
  62.             strncpy(high_score_sphere.name[4], "---------", 15);
  63.  
  64.             strncpy(high_score_time.name[0], "---------", 15);
  65.             strncpy(high_score_time.name[1], "---------", 15);
  66.             strncpy(high_score_time.name[2], "---------", 15);
  67.             strncpy(high_score_time.name[3], "---------", 15);
  68.             strncpy(high_score_time.name[4], "---------", 15);
  69.  
  70.             high_score_sphere.spheres[0] = high_score_time.spheres[0] = 30;
  71.             high_score_sphere.spheres[1] = high_score_time.spheres[1] = 35;
  72.             high_score_sphere.spheres[2] = high_score_time.spheres[2] = 40;
  73.             high_score_sphere.spheres[3] = high_score_time.spheres[3] = 45;
  74.             high_score_sphere.spheres[4] = high_score_time.spheres[4] = 50;
  75.  
  76.             high_score_sphere.time[0] = high_score_time.time[0] = 5.0f;
  77.             high_score_sphere.time[1] = high_score_time.time[1] = 4.0f;
  78.             high_score_sphere.time[2] = high_score_time.time[2] = 3.0f;
  79.             high_score_sphere.time[3] = high_score_time.time[3] = 2.0f;
  80.             high_score_sphere.time[4] = high_score_time.time[4] = 1.0f;
  81.         }
  82.     else
  83.         {
  84.             // Get sound value
  85.              next_char = fgetc(stream);
  86.  
  87.             while (next_char != '=')
  88.                 next_char = fgetc(stream);
  89.  
  90.             fgetc(stream);
  91.             next_char = fgetc(stream);
  92.  
  93.             if (next_char == 48)
  94.                 menu_options.Set_Current_Choice(0, 0);                // Sound = On
  95.             else
  96.                 menu_options.Set_Current_Choice(0, 1);                // Sound = Off
  97.  
  98.             // Get resolution
  99.             next_char = fgetc(stream);
  100.  
  101.             while (next_char != '=')
  102.                 next_char = fgetc(stream);
  103.  
  104.             fgetc(stream);
  105.             next_char = fgetc(stream);
  106.  
  107.             if (next_char == 48)
  108.                 {
  109.                     window.screen_height = 480;
  110.                     window.screen_width = 640;
  111.                     menu_options.Set_Current_Choice(1, 0);
  112.                 }
  113.             else if (next_char == 49)
  114.                 {
  115.                     window.screen_height = 600;
  116.                     window.screen_width = 800;
  117.                     menu_options.Set_Current_Choice(1, 1);
  118.                 }
  119.             else
  120.                 {
  121.                     window.screen_height = 768;
  122.                     window.screen_width = 1024;
  123.                     menu_options.Set_Current_Choice(1, 2);
  124.                 }
  125.  
  126.             // Get timed value
  127.             next_char = fgetc(stream);
  128.  
  129.             while (next_char != '=')
  130.                 next_char = fgetc(stream);
  131.  
  132.             fgetc(stream);
  133.             next_char = fgetc(stream);
  134.  
  135.             if (next_char == 48)
  136.                 menu_options.Set_Current_Choice(2, 0);                // Timed amount = 5;
  137.             else if (next_char == 49)
  138.                 menu_options.Set_Current_Choice(2, 1);                // Timed amount = 10;
  139.             else
  140.                 menu_options.Set_Current_Choice(2, 2);                // Timed amount = 15;
  141.  
  142.             // Get countdown value
  143.             next_char = fgetc(stream);
  144.  
  145.             while (next_char != '=')
  146.                 next_char = fgetc(stream);
  147.  
  148.             fgetc(stream);
  149.             next_char = fgetc(stream);
  150.  
  151.             if (next_char == 48)
  152.                 menu_options.Set_Current_Choice(3, 0);                // Countdown amount = 300
  153.             else if (next_char == 49)
  154.                 menu_options.Set_Current_Choice(3, 1);                // Countdown amount = 600
  155.             else
  156.                 menu_options.Set_Current_Choice(3, 2);                // Countdown amount = 900
  157.  
  158.             // Get name
  159.             next_char = fgetc(stream);
  160.  
  161.             while (next_char != '=')
  162.                 next_char = fgetc(stream);
  163.  
  164.             fgetc(stream);
  165.             fscanf(stream, "%15s", high_score_name);
  166.  
  167.             // Get top control
  168.             next_char = fgetc(stream);
  169.  
  170.             while (next_char != '=')
  171.                 next_char = fgetc(stream);
  172.  
  173.             fgetc(stream);
  174.             next_char = fgetc(stream);
  175.  
  176.             controller_value = next_char - 48;
  177.  
  178.             if ((controller_value < Keyboard_1) || (controller_value > No_User))
  179.                 menu_control.Set_Current_Choice(Up, Computer);
  180.             else
  181.                 {
  182.                     if (connected[controller_value] == false)
  183.                         menu_control.Set_Current_Choice(Up, Computer);
  184.                     else
  185.                         menu_control.Set_Current_Choice(Up, controller_value);
  186.                 }
  187.  
  188.             // Get bottom control
  189.             next_char = fgetc(stream);
  190.  
  191.             while (next_char != '=')
  192.                 next_char = fgetc(stream);
  193.  
  194.             fgetc(stream);
  195.             next_char = fgetc(stream);
  196.  
  197.             controller_value = next_char - 48;
  198.  
  199.             if ((controller_value < Keyboard_1) || (controller_value > No_User))
  200.                 menu_control.Set_Current_Choice(Down, Computer);
  201.             else
  202.                 {
  203.                     if (connected[controller_value] == false)
  204.                         menu_control.Set_Current_Choice(Down, Computer);
  205.                     else
  206.                         menu_control.Set_Current_Choice(Down, controller_value);
  207.                 }
  208.  
  209.             // Get left control
  210.             next_char = fgetc(stream);
  211.  
  212.             while (next_char != '=')
  213.                 next_char = fgetc(stream);
  214.  
  215.             fgetc(stream);
  216.             next_char = fgetc(stream);
  217.  
  218.             controller_value = next_char - 48;
  219.  
  220.             if ((controller_value < Keyboard_1) || (controller_value > No_User))
  221.                 menu_control.Set_Current_Choice(Left, Computer);
  222.             else
  223.                 {
  224.                     if (connected[controller_value] == false)
  225.                         menu_control.Set_Current_Choice(Left, Computer);
  226.                     else
  227.                         menu_control.Set_Current_Choice(Left, controller_value);
  228.                 }
  229.  
  230.             // Get right control
  231.             next_char = fgetc(stream);
  232.  
  233.             while (next_char != '=')
  234.                 next_char = fgetc(stream);
  235.  
  236.             fgetc(stream);
  237.             next_char = fgetc(stream);
  238.  
  239.             controller_value = next_char - 48;
  240.  
  241.             if ((controller_value < Keyboard_1) || (controller_value > No_User))
  242.                 menu_control.Set_Current_Choice(Right, Computer);
  243.             else
  244.                 {
  245.                     if (connected[controller_value] == false)
  246.                         menu_control.Set_Current_Choice(Right, Computer);
  247.                     else
  248.                         menu_control.Set_Current_Choice(Right, controller_value);
  249.                 }
  250.  
  251.             // Get compete control
  252.             next_char = fgetc(stream);
  253.  
  254.             while (next_char != '=')
  255.                 next_char = fgetc(stream);
  256.  
  257.             fgetc(stream);
  258.             next_char = fgetc(stream);
  259.  
  260.             controller_value = next_char - 48;
  261.  
  262.             if ((controller_value < Keyboard_1) || (controller_value > Joystick_4))
  263.                 menu_control.Set_Current_Choice(Compete, Computer);
  264.             else
  265.                 {
  266.                     if (connected[controller_value] == false)
  267.                         menu_control.Set_Current_Choice(Compete, Keyboard_1);
  268.                     else
  269.                         menu_control.Set_Current_Choice(Compete, controller_value);
  270.                 }
  271.  
  272.             // Get high scores by spheres
  273.             for (x1=0;x1<5;x1=x1+1)
  274.                 {
  275.                      next_char = fgetc(stream);
  276.                     while (next_char != '=')
  277.                         next_char = fgetc(stream);
  278.  
  279.                      fgetc(stream);
  280.                     fscanf(stream, "%15s", high_score_sphere.name[x1]);
  281.  
  282.                      next_char = fgetc(stream);
  283.                     while (next_char != '=')
  284.                         next_char = fgetc(stream);
  285.  
  286.                      fgetc(stream);
  287.                     fscanf(stream, "%i", &high_score_sphere.spheres[x1]);
  288.  
  289.                      next_char = fgetc(stream);
  290.                     while (next_char != '=')
  291.                         next_char = fgetc(stream);
  292.  
  293.                      fgetc(stream);
  294.                     fscanf(stream, "%f", &high_score_sphere.time[x1]);
  295.                 }
  296.  
  297.             // Get high scores by time
  298.             for (x1=0;x1<5;x1=x1+1)
  299.                 {
  300.                      next_char = fgetc(stream);
  301.                     while (next_char != '=')
  302.                         next_char = fgetc(stream);
  303.  
  304.                      fgetc(stream);
  305.                     fscanf(stream, "%15s", high_score_time.name[x1]);
  306.  
  307.                      next_char = fgetc(stream);
  308.                     while (next_char != '=')
  309.                         next_char = fgetc(stream);
  310.  
  311.                      fgetc(stream);
  312.                     fscanf(stream, "%i", &high_score_time.spheres[x1]);
  313.  
  314.                      next_char = fgetc(stream);
  315.                     while (next_char != '=')
  316.                         next_char = fgetc(stream);
  317.  
  318.                      fgetc(stream);
  319.                     fscanf(stream, "%f", &high_score_time.time[x1]);
  320.                 }
  321.  
  322.             fclose(stream);
  323.         }
  324.  
  325.     // Count players
  326.     values.player_number = 0;
  327.  
  328.     for (x1=Up;x1<=Right;x1=x1+1)
  329.         {
  330.             if (menu_control.Get_Current_Choice(x1) != No_User)
  331.                 values.player_number = values.player_number + 1;
  332.         }
  333.  
  334.     // Adjust if less than 2 players (add computer)
  335.     if (values.player_number <= 1)
  336.         {
  337.             menu_control.Set_Current_Choice(Up, Computer);
  338.             menu_control.Set_Current_Choice(Down, Computer);
  339.         }
  340. }
  341.  
  342. /***************************************************************************/
  343.  
  344. void Save_Config_File()
  345.  
  346. {
  347.     FILE    *stream;
  348.     int        x1;
  349.  
  350.     stream = fopen("config.ini", "w");
  351.  
  352.     fprintf(stream, "-----------------------------------------------------------------\n\n");
  353.     fprintf(stream, "Sound (ON, OFF) = %i\n", menu_options.Get_Current_Choice(0));
  354.     fprintf(stream, "Resolution (640, 800, 1024) = %i\n", menu_options.Get_Current_Choice(1));
  355.     fprintf(stream, "Timed (5, 10, 15) = %i\n",    menu_options.Get_Current_Choice(2));
  356.     fprintf(stream, "Countdown (300, 600, 900) = %i\n",    menu_options.Get_Current_Choice(3));
  357.     fprintf(stream, "Name = %s\n", high_score_name);
  358.     fprintf(stream, "Top = %i\n", menu_control.Get_Current_Choice(Up));
  359.     fprintf(stream, "Bottom = %i\n", menu_control.Get_Current_Choice(Down));
  360.     fprintf(stream, "Left = %i\n", menu_control.Get_Current_Choice(Left));
  361.     fprintf(stream, "Right = %i\n", menu_control.Get_Current_Choice(Right));
  362.     fprintf(stream, "Compete = %i\n\n", menu_control.Get_Current_Choice(Compete));
  363.     fprintf(stream, "-----------------------------------------------------------------\n\n");
  364.  
  365.     for (x1=0;x1<5;x1=x1+1)
  366.         {
  367.             fprintf(stream, "Hiscore %i", x1 + 1);
  368.             fprintf(stream, "a = %s = %i = %.2f\n", high_score_sphere.name[x1], high_score_sphere.spheres[x1], high_score_sphere.time[x1]);
  369.         }
  370.  
  371.     fprintf(stream, "\n");
  372.  
  373.     for (x1=0;x1<5;x1=x1+1)
  374.         {
  375.             fprintf(stream, "Hiscore %i", x1 + 1);
  376.             fprintf(stream, "b = %s = %i = %.2f\n", high_score_time.name[x1], high_score_time.spheres[x1], high_score_time.time[x1]);
  377.         }
  378.  
  379.     fprintf(stream, "\n");
  380.     fprintf(stream, "-----------------------------------------------------------------\n");
  381.     fclose(stream);
  382. }
  383.  
  384. /***************************************************************************/